/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fase2.pt.iul.sid.view;

import fase2.pt.iul.sid.controller.PesquisaController;
import fase2.pt.iul.sid.model.ComentarioAoEstabelecimentoModel;
import fase2.pt.iul.sid.model.ComentarioAoPratoModel;
import fase2.pt.iul.sid.model.EstabelecimentoModel;
import fase2.pt.iul.sid.model.PratoModel;
import fase2.pt.iul.sid.model.UtilizadorModel;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;


/**
 *
 * @author Ricardo Marques
 */
public class Pesquisa extends javax.swing.JFrame {

    private Vector<Vector<Object>> auxVectorSelection = null;
    private DefaultTableModel model;
    /**
     * Creates new form Teste
     * @param lista_utilizadores
     * @param lista_estabelecimento
     */
    public Pesquisa(List<UtilizadorModel> lista_utilizadores, List<EstabelecimentoModel> lista_estabelecimento, List<PratoModel> lista_pratos) {
        initComponents();  
        
        auxVectorSelection = new Vector<Vector<Object>>();
        
        DefaultTableModel model = new DefaultTableModel(null, getColumnsNames());
        jTable1.setModel(model);
        //jTable1.setEnabled(false);
        jTable1.setRowSelectionAllowed(true);
        jTable1.setColumnSelectionAllowed(false);
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        //Carregar combobox's com informação
        jComboBox1.setModel(new DefaultComboBoxModel(lista_utilizadores.toArray())); 
        jComboBox2.setModel(new DefaultComboBoxModel(lista_estabelecimento.toArray())); 
        jComboBox3.setModel(new DefaultComboBoxModel(lista_pratos.toArray()));
        
        this.setResizable(false);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Utilizador");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Estabelecimento");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jLabel3.setText("Prato");

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Comentário");

        jLabel5.setText("Avaliação >=");

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5" }));

        jLabel6.setText("Foto");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        )
        {public boolean isCellEditable(int row, int column){return false;}}
    );
    jScrollPane1.setViewportView(jTable1);

    jButton3.setText("Ver Comentário / avaliação");

    jButton4.setText("Ver fotografia");

    jButton5.setText("Sair");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jSeparator1)
        .addGroup(layout.createSequentialGroup()
            .addGap(29, 29, 29)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(258, 258, 258)
                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 766, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel5))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jComboBox4, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBox2, 0, 200, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox3, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))))
            .addContainerGap(29, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(39, 39, 39)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel3)
                .addComponent(jLabel1)
                .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addComponent(jCheckBox1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(1, 1, 1)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap())
    );

    jComboBox1.getAccessibleContext().setAccessibleName("comBoxUtilizadores");
    jButton3.getAccessibleContext().setAccessibleName("VerComentário");
    jButton4.getAccessibleContext().setAccessibleName("VerFotografia");
    jButton4.getAccessibleContext().setAccessibleDescription("");

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed
    
      
    public void adcionarListner(ActionListener listner){
        
        this.jComboBox1.addActionListener(listner);
        this.jComboBox2.addActionListener(listner);
        this.jComboBox3.addActionListener(listner);
        this.jComboBox4.addActionListener(listner);
        this.jCheckBox1.addActionListener(listner);
        this.jTextField1.addActionListener(listner);
        
    }
    
    public void addBtnActionLister(ActionListener listner){
        
        this.jButton3.addActionListener(listner);
        this.jButton4.addActionListener(listner);
        this.jButton5.addActionListener(listner);
        
    }

    public JComboBox getjComboBox1() {
        return jComboBox1;
    }
    
    public JComboBox getjComboBox2() {
        return jComboBox2;
    }
     
    public JComboBox getjComboBox3() {
        return jComboBox3;
    }
      
    public JComboBox getjComboBox4() {
        return jComboBox4;
    }

    public JCheckBox getjCheckBox1() {
        return jCheckBox1;
    }

    public JTextField getjTextField1() {
        return jTextField1;
    }

    public JButton getjButton3() {
        return jButton3;
    }

    public JButton getjButton4() {
        return jButton4;
    }

    public JButton getjButton5() {
        return jButton5;
    }
    
    public JTable getjTable1() {
        return jTable1;
    }

    public Vector<Vector<Object>> getAuxVectorSelection() {
        return auxVectorSelection;
    }    
    
    public void mostraComentarios(List<ComentarioAoEstabelecimentoModel> lista_comentarios_estabelecimento, List<ComentarioAoPratoModel> lista_comentarios_pratos){
        
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        auxVectorSelection.clear();
        
        for (ComentarioAoEstabelecimentoModel estabelecimento : lista_comentarios_estabelecimento) {
            
            Vector<Object> vObj = new Vector<Object>();
            vObj.add(estabelecimento.getUserName());
            vObj.add(estabelecimento.getUserEmail());
            vObj.add(estabelecimento.getEstabelicimento());
            vObj.add("");
            vObj.add(estabelecimento.getNota());
            vObj.add(estabelecimento.getComentario());
            
            //adicionar imagem a jtable
            
            ImageIcon Icon = new ImageIcon(this.getClass().getResource("/fase2/pt/iul/sid/images/certo.png"));
            if(estabelecimento.isFoto()){
                vObj.add(Icon);
            }else{
                vObj.add("");
            }
            
            data.add(vObj);
            
            //tabela auxiliar
            vObj = new Vector<Object>();
            vObj.add(estabelecimento.getIdEstabelicimento());
            vObj.add(0);
            auxVectorSelection.add(vObj);
                    
        }  
        
        for (ComentarioAoPratoModel prato : lista_comentarios_pratos) {
            
            Vector<Object> vObj = new Vector<Object>();
            vObj.add(prato.getUserName());
            vObj.add(prato.getUserEmail());
            vObj.add(prato.getEstabelicimento());
            vObj.add(prato.getPrato());
            vObj.add(prato.getNota());
            vObj.add(prato.getComentario());
            
            //adicionar imagem a jtable
            ImageIcon Icon = new ImageIcon(this.getClass().getResource("/fase2/pt/iul/sid/images/certo.png"));
            if(prato.isFoto()){
                vObj.add(Icon);
            }else{
                vObj.add("");
            }
            
            data.add(vObj);
            
            //tabela auxiliar
            vObj = new Vector<Object>();
            vObj.add(prato.getIdEstabelecimento());
            vObj.add(prato.getIdPrato());
            auxVectorSelection.add(vObj);
            
        }  
              
        //Permite que a tabela não seja editavel
        model = new DefaultTableModel(data, getColumnsNames()) {
            @Override
            public boolean isCellEditable(int row, int column) {
               return false;
            }
        };
        jTable1.setModel(model);
        jTable1.getColumnModel().getColumn(6).setCellRenderer(jTable1.getDefaultRenderer(ImageIcon.class));
        tableSorter();
        
    }
    
    private Vector<String> getColumnsNames(){
        
        Vector<String> columnNames = new Vector<String>();
        columnNames.add("Utilizador");
        columnNames.add("Email");
        columnNames.add("Estabelecimento");
        columnNames.add("Prato");
        columnNames.add("Aval.");
        columnNames.add("Comentário");
        columnNames.add("Foto");
        return columnNames;
        
    }

    public DefaultTableModel getModel() {
        return model;
    }
    
    private void tableSorter(){
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model) {
            @Override public void toggleSortOrder(int column) {
                //if(column>=0 && column<getModelWrapper().getColumnCount() && isSortable(column)) {
                if( column>=0 && column<getModelWrapper().getColumnCount() && isSortable(column)) {
                    List<RowSorter.SortKey> keys = new ArrayList<RowSorter.SortKey>(getSortKeys());
                    if(!keys.isEmpty()) {
                        RowSorter.SortKey sortKey = keys.get(0);
                        if(sortKey.getColumn()==column && sortKey.getSortOrder()==SortOrder.DESCENDING) {
                            setSortKeys(null);
                            return;
                        }
                    }
                }
                super.toggleSortOrder(column);
            }
        };
        jTable1.setRowSorter(sorter);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    

}
